package com.bluesky.jwf.component.window;

import java.util.LinkedList;
import java.util.Queue;

public class Dialog extends Window {

	private boolean modal = false;

	protected ModalResult modalResult;

	@Override
	public void init(String html) {

		super.init(html);

		windowTag.setStyleAttribute("position", "absolute");
		windowTag.setStyleAttribute("border", "1px solid black");
		if (windowTag.getStyleAttribute("background-color") == null)
			windowTag.setStyleAttribute("background-color", "white");
	}

	public void show() {
		modal = false;
	}

	/**
	 * 
	 */
	public ModalResult showModal() {
		this.modal = true;

		WindowThread thread = (WindowThread) Thread.currentThread();
		Queue<Event> q = new LinkedList<Event>();

		thread.serveNewQueue(q);

		return modalResult;
	}

	public ModalResult getModalResult() {
		return modalResult;
	}

	private void setModalResult(ModalResult modalResult) {
		this.modalResult = modalResult;

		if (modal) {
			WindowThread thread = (WindowThread) Thread.currentThread();
			thread.putExitQueueEvent();

		}
	}

	@Override
	public void close() {

		super.close();

		// this will end the modal dialog.
		if (modal)
			setModalResult(modalResult);
	}

}
